package com.woniu.elderly.wares.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.WaresDTO;
import com.woniu.common.dto.WaresSpuDTO;
import com.woniu.common.entity.WaresSpu;
import com.woniu.common.vo.SpuVO;
import com.woniu.elderly.wares.mapper.WaresSkuMapper;
import com.woniu.elderly.wares.service.WaresSpuService;
import com.woniu.elderly.wares.mapper.WaresSpuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【wares_spu】的数据库操作Service实现
* @createDate 2024-08-02 11:57:49
*/
@Service
public class WaresSpuServiceImpl extends ServiceImpl<WaresSpuMapper, WaresSpu>
    implements WaresSpuService{

    @Autowired
    private WaresSpuMapper waresSpuMapper;
    @Autowired
    private WaresSkuMapper waresSkuMapper;
    @Override
    public WaresSpuDTO getWaresSpuDTO(Integer sid) {
        //判空
        if(sid == null){
            throw new RuntimeException("sid不能为空");
        }
        WaresSpuDTO waresSpuDTO = new WaresSpuDTO();
        WaresSpu waresSpu = waresSpuMapper.selectById(sid);
        //赋值
        waresSpuDTO.setSid(waresSpu.getSid());
        waresSpuDTO.setSname(waresSpu.getSname());
        waresSpuDTO.setTitle(waresSpu.getTitle());
        waresSpuDTO.setDescription(waresSpu.getDescription());
        waresSpuDTO.setCategoryId(waresSpu.getCategoryId());
        waresSpuDTO.setStatus(waresSpu.getStatus());
        waresSpuDTO.setCreateTime(waresSpu.getCreateTime());
        //查下属列表
        List<WaresDTO> waresSkuDTO = waresSkuMapper.getWaresSkuDTOBySid(sid);
        waresSpuDTO.setSkuList(waresSkuDTO);
        return waresSpuDTO;
    }

    @Override
    public List<SpuVO> getSpuVO() {
        List<WaresSpu> waresSpus = waresSpuMapper.selectList(null);
        List<SpuVO> spuVOS = waresSpus.stream().map(waresSpu -> {
            SpuVO spuVO = new SpuVO();
            spuVO.setSpuId(waresSpu.getSid());
            spuVO.setValue(waresSpu.getTitle());
            spuVO.setSpuName(waresSpu.getSname());
            return spuVO;
        }).collect(Collectors.toList());
        return spuVOS;
    }
}




